﻿using AG_Apps_Cloud_Services_WCF_Web_Role;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BuienradarAppWeb.Pages
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            String language = Request.QueryString["SPLanguage"];

            if (!String.IsNullOrEmpty(language))
            {
                CultureInfo ci = new CultureInfo(language);

                System.Threading.Thread.CurrentThread.CurrentCulture = ci;
                System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
            }
            String user = "";
            String contextTokenString = TokenHelper.GetContextTokenFromRequest(Request);

            if (!String.IsNullOrEmpty(contextTokenString ))
            {
                var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

                using (var clientContext = spContext.CreateUserClientContextForSPHost())
                {
                    clientContext.Load(clientContext.Web, web => web.CurrentUser);
                    clientContext.ExecuteQuery();

                    user = clientContext.Web.CurrentUser.Email;
                }
            }

            String script = @"
var adRotator;
var analytics;

$(document).ready(function () {
    adRotator= new ArthurGraus.AdRotator('BRDR',10000, 'adRotator', 1);
    adRotator.initialize();

    analytics = new ArthurGraus.Analytics('BRDR');
    analytics.email= '" + user + @"';
    analytics.initialize();
});";

                    this.ClientScript.RegisterStartupScript(this.GetType(), "AdRotator", script, true);

        }
    }
}